package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.hosp.bean.Result;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.utils.MD5;
import com.atguigu.yygh.model.hosp.Department;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author luoyin
 * @Date 15:03 2022/9/27
 **/
@RestController
@RequestMapping("/api/hosp")
@Api("科室信息设置")
public class ApiDepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @PostMapping("/saveDepartment")
    public Result<Department> saveDepartment(@RequestParam Map<String,String> paramMap){
        //校验signKey
        String sign = paramMap.get("sign");
        String hoscode = paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKeyByHoscode(hoscode);
        String signMD5 = MD5.encrypt(signKey);
        if(!sign.equals(signMD5)){
            throw new YyghException(201,"校验不匹配");
        }
        //保存科室
        departmentService.save(paramMap);
        return Result.ok();
    }

    @PostMapping("/department/list")
    public Result page(@RequestParam Map<String,String> paramMap){
        //校验signKey
        String sign = paramMap.get("sign");
        String hoscode = paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKeyByHoscode(hoscode);
        String signMD5 = MD5.encrypt(signKey);
        if(!sign.equals(signMD5)){
            throw new YyghException(201,"校验不匹配");
        }

        Page<Department> page = departmentService.getDepartmentPage(paramMap);
        return Result.ok(page);
    }

    @PostMapping("/department/remove")
    public Result remove(@RequestParam Map<String,String> paramMap){
        //检验signkey
        String sign = paramMap.get("sign");
        String hoscode = paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKeyByHoscode(hoscode);
        String signMD5 = MD5.encrypt(signKey);
        if(!sign.equals(signMD5)){
            throw new YyghException(201,"校验不匹配");
        }

        departmentService.remove(hoscode,paramMap.get("depcode"));
        return Result.ok();
    }
}
